/*
 * @Author: 黑白灰01 3193273703@qq.com
 * @Date: 2025-01-27 13:18:02
 * @LastEditors: 黑白灰01 3193273703@qq.com
 * @LastEditTime: 2025-01-30 17:33:45
 * @FilePath: /hei_picture_web/src/stores/userLoginStore.ts
 * @Description: 用户登录信息状态
 */
import { getLoginUserUsingGet } from '@/api/userController'
import { defineStore } from 'pinia'
import { ref } from 'vue'
type LoginUserInfo = {
  userName: string
  userId: string | null
  userAvatar: string | null
  userRole: string | null
}
export const useLoginUserStore = defineStore('loginUser', () => {
  const loginUserInfo = ref<LoginUserInfo>({
    userName: '',
    userId: null,
    userAvatar: null,
    userRole: null,
  })
  /**
   * @description: 获取登录用户信息
   */
  const fetchLoginUserInfo = async () => {
    const { data } = await getLoginUserUsingGet({})
    if (data.code === 200) {
      loginUserInfo.value.userId = data.data?.id ?? null
      loginUserInfo.value.userName = data.data?.userName ?? ''
      loginUserInfo.value.userAvatar = data.data?.userAvatar ?? null
      loginUserInfo.value.userRole = data.data?.userRole ?? null
    }
  }
  /**
   * @description: 设置登录用户信息
   */
  const setLoginUserInfo = (userInfo: LoginUserInfo) => {
    loginUserInfo.value = userInfo
  }
  return { loginUserInfo, fetchLoginUserInfo, setLoginUserInfo }
})
